package com.gitee.wsl.jvm.okhttp.response

import okio.Sink
import okio.sink
import java.io.File

/**
 * A resource that supports writing to it.
 */
interface WritableResource {
    /**
     * The byte length of this resource.
     * If the substance for this resource does not exist, this value is `0`.
     */
    val length: Long

    /**
     * Opens a new [Sink] that writes to this resource.
     * If the substance for this resource does not exist, it will be created.
     *
     * @param append If `true`, data will be written to the end of the resource rather than
     * the beginning.
     */
    fun newSink(append: Boolean): Sink

    companion object {
        /**
         * Returns a [WritableResource] that represents the given File.
         */
        fun File.asWritableResource(): WritableResource = FileWritableResource(this)

        private class FileWritableResource(private val file: File) : WritableResource {
            override val length: Long
                get() = file.length()

            override fun newSink(append: Boolean): Sink = file.sink(append)
        }
    }
}